package com.zhang.modules.user.entity;

import com.zhang.common.jpa.entity.BaseEntity;
import com.zhang.modules.auth.eneity.Role;
import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import org.hibernate.validator.constraints.NotBlank;

import javax.persistence.*;
import java.time.Instant;
import java.util.Set;

@AllArgsConstructor
@NoArgsConstructor
@Getter
@Setter
@Entity(name = "SysUser")
@Table(name = "sys_user")
public class SysUser extends BaseEntity {

    @Column(name = "dept_id")
    @ApiModelProperty("部门ID")
    private Long deptId;

    @Column(name = "user_account", nullable = false, length = 30)
    @ApiModelProperty("用户账号")
    @NotBlank(message = "用户账号不能为空")
    private String userAccount;

    @Column(name = "nick_name", nullable = false, length = 30)
    @ApiModelProperty("用户昵称")
    @NotBlank(message = "用户昵称不能为空")
    private String nickName;

    @Column(name = "user_type", length = 2)
    @ApiModelProperty("用户类型")
    private String userType = "0";

    @Column(name = "email", length = 50)
    @ApiModelProperty("用户邮箱")
    private String email;

    @Column(name = "phonenumber", length = 11)
    @ApiModelProperty("手机号码")
    private String phonenumber;

    @Column(name = "sex")
    @ApiModelProperty("用户性别（0男 1女 2未知）")
    private Character sex = '0';

    @Column(name = "avatar", length = 100)
    @ApiModelProperty("头像地址")
    private String avatar;

    @Column(name = "password", length = 100)
    @ApiModelProperty("密码")
    @NotBlank(message = "密码不能为空")
    private String password;

    @Column(name = "password_salt", nullable = false, length = 128)
    @ApiModelProperty("用户密码盐值")
    private String passwordSalt;

    @Column(name = "need_set_password", nullable = false)
    @ApiModelProperty("是否需要重新设置密码")
    private Boolean needSetPassword = false;

    @Column(name = "status")
    @ApiModelProperty("帐号状态（0锁定 1启用）")
    private Character status = '1';

    @Column(name = "login_ip", length = 128)
    @ApiModelProperty("最后登录IP")
    private String loginIp;

    @Column(name = "login_date")
    @ApiModelProperty("最后登录时间")
    private Instant loginDate;

    @Column(name = "remark", length = 500)
    @ApiModelProperty("备注")
    private String remark;


    @ManyToMany
    @JoinTable(
            name = "sys_acl_user_role",
            joinColumns = @JoinColumn(name = "user_id"),
            inverseJoinColumns = {
                    @JoinColumn(name = "role_id",referencedColumnName = "id"),
                    @JoinColumn(name = "id",referencedColumnName = "id")
            }
    )
    private Set<Role> roles;

}